package jp.co.iris_ltd.irisphotoeditor.util;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;

public class StampArtTool extends ArtTool {
    private Drawable mStampDrawable;
    private Bitmap mStampBitmap;
    private boolean isReady = false;

    public StampArtTool(Drawable stampDrawable) {
        mStampDrawable = stampDrawable;

        init();
    }

    private void init() {
        mMode = ToolMode.Stamp;
        mPaint = new Paint();

        mStampBitmap = ((BitmapDrawable) mStampDrawable).getBitmap();
    }

    @Override
    public void setPosition(MotionEvent event, float scale) {
        mX = event.getX() / scale;
        mY = (event.getY() / scale - 60 / scale);
        isReady = true;
    }

    @Override
    public void drawCanvas(Canvas canvas) {
        if (isReady) {
            if (!isFinalized()) {
                canvas.drawBitmap(mStampBitmap, mX - mStampBitmap.getWidth() / 2, mY - mStampBitmap.getHeight() / 2, mPaint);
            } else {
                canvas.drawBitmap(mStampBitmap, mX + mOffsetX - mStampBitmap.getWidth() / 2, mY + mOffsetY - mStampBitmap.getHeight() / 2, mPaint);
            }
        }
    }

    @Override
    public void restoreCanvas(Canvas canvas) {
        mStampBitmap = ((BitmapDrawable) mStampDrawable).getBitmap();
        canvas.drawBitmap(mStampBitmap, mX - mStampBitmap.getWidth() / 2, mY - mStampBitmap.getHeight() / 2, mPaint);
    }

    @Override
    public void onDrawingFinish() {
        isReady = false;
        mStampBitmap.recycle();
    }
}
